// MobileFlashLight.cpp : Defines the entry point for the application.

#include "stdafx.h"
#include "MobileFlashLight.h"

// Stores the current colour of the flashlight
static COLORREF gbl_flashColour = RGB(255, 255, 0);

// Message handler for about box.
INT_PTR CALLBACK SettingsDlg(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
        case WM_INITDIALOG:
            {
                // Create a Done button and size it.  
                SHINITDLGINFO shidi;
                shidi.dwMask = SHIDIM_FLAGS;
                shidi.dwFlags = SHIDIF_DONEBUTTON | SHIDIF_SIPDOWN | SHIDIF_SIZEDLGFULLSCREEN | SHIDIF_EMPTYMENU;
                shidi.hDlg = hDlg;
                SHInitDialog(&shidi);

				// Set the min and max range for each of the R,G and B spin boxes
				SendDlgItemMessage(hDlg, IDC_SPIN_RED, UDM_SETRANGE32, 0, 255);
				SendDlgItemMessage(hDlg, IDC_SPIN_RED, UDM_SETPOS, 0, MAKELPARAM(GetRValue(gbl_flashColour), 0));
				SendDlgItemMessage(hDlg, IDC_SPIN_GREEN, UDM_SETRANGE32, 0, 255);
				SendDlgItemMessage(hDlg, IDC_SPIN_GREEN, UDM_SETPOS, 0, MAKELPARAM(GetGValue(gbl_flashColour), 0));
				SendDlgItemMessage(hDlg, IDC_SPIN_BLUE, UDM_SETRANGE32, 0, 255);
				SendDlgItemMessage(hDlg, IDC_SPIN_BLUE, UDM_SETPOS, 0, MAKELPARAM(GetBValue(gbl_flashColour), 0));
            }
            return (INT_PTR)TRUE;

		case WM_CTLCOLORSTATIC:
			if (GetWindowLong((HWND)lParam, GWL_ID) == IDC_COLOURBOX)
			{
				// Make the colour box the same colour specified
				// via the R, G and B spin boxes.
				int r = GetDlgItemInt(hDlg, IDC_EDIT_RED, NULL, FALSE);
				int g = GetDlgItemInt(hDlg, IDC_EDIT_GREEN, NULL, FALSE);
				int b = GetDlgItemInt(hDlg, IDC_EDIT_BLUE, NULL, FALSE);

				// [CF] - TODO - don't leak GDI resources...
				HBRUSH brush = CreateSolidBrush(RGB(r, g, b));
				return (INT_PTR)brush;
			}
			break;

        case WM_COMMAND:
			switch (LOWORD(wParam))
			{
			case IDC_EDIT_RED:
			case IDC_EDIT_GREEN:
			case IDC_EDIT_BLUE:
				if (HIWORD(wParam) == EN_UPDATE)
				{
					// If any of the R, G and B spin boxes change value
					// then cause the colour box to be repainted.
					InvalidateRect(GetDlgItem(hDlg, IDC_COLOURBOX), NULL, TRUE);
				}
				break;

			case IDOK:
            {
				// Read the current R, G and B values and store them
				// within our global variable
				int r = GetDlgItemInt(hDlg, IDC_EDIT_RED, NULL, FALSE);
				int g = GetDlgItemInt(hDlg, IDC_EDIT_GREEN, NULL, FALSE);
				int b = GetDlgItemInt(hDlg, IDC_EDIT_BLUE, NULL, FALSE);

				gbl_flashColour = RGB(r, g, b);

                EndDialog(hDlg, LOWORD(wParam));
                return (INT_PTR)TRUE;
            }
			break;
			}
            break;
    }
    return (INT_PTR)FALSE;
}

INT_PTR CALLBACK FlashLightDlg(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
        case WM_INITDIALOG:
            {
                // Create a Done button and size it.  
                SHINITDLGINFO shidi;
				SHMENUBARINFO mbi;
                shidi.dwMask = SHIDIM_FLAGS;
                shidi.dwFlags = SHIDIF_DONEBUTTON | SHIDIF_SIPDOWN | SHIDIF_SIZEDLGFULLSCREEN;
                shidi.hDlg = hDlg;
                SHInitDialog(&shidi);

				// Create a menu bar
				memset(&mbi, 0, sizeof(SHMENUBARINFO));
				mbi.cbSize     = sizeof(SHMENUBARINFO);
				mbi.hwndParent = hDlg;
				mbi.nToolBarId = IDR_MENU;
				mbi.hInstRes   = GetModuleHandle(NULL);
				mbi.dwFlags = SHCMBF_HMENU;
				SHCreateMenuBar(&mbi);
            }
            return (INT_PTR)TRUE;

		case WM_CTLCOLORDLG:
			{
				// Paint the entire background of the window
				// with the current flashlight colour
				HBRUSH brush = CreateSolidBrush(gbl_flashColour);
				return (INT_PTR)brush;
			}
			break;

        case WM_COMMAND:
			switch (LOWORD(wParam))
			{
			case IDOK:
                EndDialog(hDlg, LOWORD(wParam));
                return (INT_PTR)TRUE;

			case IDC_SETTINGS:
				// Display the settings dialog
				DialogBox(GetModuleHandle(NULL), (LPCTSTR)IDD_SETTINGS, hDlg, SettingsDlg);

				// Invalidate the window so that it will be
				// repainted with the newly selected colour
				InvalidateRect(hDlg, NULL, TRUE);
				return (INT_PTR)TRUE;
			}
            break;

        case WM_CLOSE:
            EndDialog(hDlg, message);
            return (INT_PTR)TRUE;

    }
    return (INT_PTR)FALSE;
}

int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPTSTR    lpCmdLine,
                   int       nCmdShow)
{
	return DialogBox(hInstance, (LPCTSTR)IDD_FLASHLIGHT, NULL, FlashLightDlg);
}